home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / VIEWS.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  3KB  |  172 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef SGI
  4. #include "gl.h"
  5. #include "device.h"
  6. #else
  7. #include "vogl.h"
  8. #include "vodevice.h"
  9. #endif
  10.  
  11. /*
  12.  * drawtetra
  13.  *
  14.  *    generate a tetraedron as a series of move draws
  15.  */
  16. void
  17. drawtetra()
  18. {
  19.     
  20.     move(-0.5,  0.866, -0.5);
  21.     draw(-0.5, -0.866, -0.5);
  22.     draw( 1.0,  0.0,   -0.5);
  23.     draw(-0.5,  0.866, -0.5);
  24.     draw( 0.0,  0.0,    1.5);
  25.     draw(-0.5, -0.866, -0.5);
  26.     move( 1.0,  0.0,   -0.5);
  27.     draw( 0.0,  0.0,    1.5);
  28.     
  29.     /* 
  30.      * Label the vertices.
  31.      */
  32.     color(WHITE);
  33.     htextsize(0.3, 0.5);
  34.     move(-0.5,  0.866, -0.5);
  35.     hdrawchar('a');
  36.     move(-0.5, -0.866, -0.5);
  37.     hdrawchar('b');
  38.     move( 1.0,  0.0,   -0.5);
  39.     hdrawchar('c');
  40.     move( 0.0,  0.0,    1.5);
  41.     hdrawchar('d');
  42. }
  43.  
  44. /*
  45.  *    Shows various combinations of viewing and
  46.  *    projection transformations.
  47.  */
  48. main()
  49. {
  50.     Screencoord    minx, maxx, miny, maxy;
  51.     short        val;
  52.  
  53.     hfont("times.r");
  54.  
  55.     winopen("views");
  56.     qdevice(KEYBD);
  57.     unqdevice(INPUTCHANGE);
  58.  
  59.     color(BLACK);
  60.     clear();
  61.  
  62.     /*
  63.      * we want to draw just within the boundaries of the screen
  64.      */
  65.     getviewport(&minx, &maxx, &miny, &maxy);
  66.     viewport(maxx / 10, maxx / 10 * 9, maxy / 10, maxy / 10 * 9);
  67.  
  68.  
  69.     ortho2(-5.0, 5.0, -5.0, 5.0);    /* set the world size */
  70.  
  71.     color(RED);
  72.     rect(-4.99, -4.99, 4.99, 4.99);    /* draw a boundary frame */
  73.  
  74.     /*
  75.      * set up a perspective projection with a field of view of
  76.      * 40.0 degrees, aspect ratio of 1.0, near clipping plane 0.1,
  77.      * and the far clipping plane at 1000.0.
  78.      */
  79.     perspective(400, 1.0, 0.1, 1000.0);
  80.  
  81.     /*
  82.      * we want the drawing to be done with our eye point at (5.0, 8.0, 5.0)
  83.      * looking towards (0.0, 0.0, 0.0). The last parameter gives a twist
  84.      * in degrees around the line of sight, in this case zero.
  85.      */
  86.     lookat(5.0, 8.0, 5.0, 0.0, 0.0, 0.0, (Angle)0);
  87.  
  88.     drawtetra();
  89.  
  90.     move2(-4.5, -4.5);
  91.     htextsize(0.6, 0.9); 
  92.     hcharstr("perspective/lookat");
  93.  
  94.     qread(&val);
  95.  
  96.     /*
  97.      * window can also be used to give a perspective projection. Its
  98.      * arguments are 6 clipping planes, left, right, bottom, top, near,
  99.      * and far.
  100.      */
  101.     window(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
  102.     /*
  103.      * as window replaces the current transformation matrix we must
  104.      * specify our viewpoint again.
  105.      */
  106.     lookat(5.0, 8.0, 5.0, 0.0, 0.0, 0.0, 0.0);
  107.  
  108.     color(BLACK);
  109.     clear();
  110.  
  111.     color(GREEN);
  112.     rect(-4.99, -4.99, 4.99, 4.99);    /* draw a boundary frame */
  113.  
  114.     drawtetra();
  115.  
  116.     move2(-4.5,-4.5);
  117.     htextsize(0.6, 0.9);
  118.     hcharstr("window/lookat");
  119.  
  120.     qread(&val);
  121.  
  122.     /*
  123.      * set up our original perspective projection again.
  124.      */
  125.     perspective(400, 1.0, 0.1, 1000.0);
  126.     /*
  127.      * polarview also specifies our viewpoint, but, unlike lookat, in polar
  128.      * coordinates. Its arguments are the distance from the world origin, an
  129.      * azimuthal angle in the x-y plane measured from the y axis, an 
  130.      * incidence angle in the y-z plane measured from the z axis, and a
  131.      * twist around the line of sight.
  132.      */
  133.     polarview(15.0, 30.0, 30.0, 30.0);
  134.  
  135.     color(BLACK);
  136.     clear();
  137.  
  138.     color(MAGENTA);
  139.     rect(-4.99, -4.99, 4.99, 4.99);    /* draw a boundary frame */
  140.  
  141.     drawtetra();
  142.  
  143.     move2(-4.5,-4.5);
  144.     htextsize(0.6, 0.9);
  145.     hcharstr("perspective/polarview");
  146.  
  147.     qread(&val);
  148.  
  149.     /*
  150.      * once more with window for comparison
  151.      */
  152.     window(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);
  153.     polarview(6.0, 200, -300, 700);
  154.  
  155.     color(BLACK);
  156.     clear();
  157.  
  158.     color(YELLOW);
  159.     rect(-4.99, -4.99, 4.99, 4.99);    /* draw a boundary frame */
  160.  
  161.     drawtetra();
  162.  
  163.     move2(-4.5,-4.5);
  164.     htextsize(0.6, 0.9);
  165.     hcharstr("window/polarview");
  166.  
  167.     qread(&val);
  168.  
  169.     gexit();
  170. }
  171.  
  172.